import { ADDCOUNTER, ADDTOCART } from './mutations-type';
export default {
  addCart(context, payLoad) {
    return new Promise((resolve, reject) => {
      // payLoad新添加的商品
      let { commit, state } = context;
      let oldProduct = null;
      // for (let item of state.cartList) {
      //   if (item.iid === payLoad.iid) {
      //     oldProduct = item;
      //   }
      // }
      oldProduct = state.cartList.find((item) => {
        return item.iid === payLoad.iid
      })
      // 2.判断oldProduct
      if (oldProduct) { // 数量+1
        // oldProduct.count += 1;
        commit(ADDCOUNTER, oldProduct);
        resolve('当前商品数量+1');
      } else { // 添加新商品
        payLoad.count = 1;
        commit(ADDTOCART, payLoad);
        resolve('添加新商品');
      }
    })
  }
}
